package k.daniel.android.listview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.lidroid.xutils.ViewUtils;

/**
 * Created by daniel on 2015/3/25.
 */
public abstract class BaseArrayAdapter<T> extends BaseAdapter {
    public Context context;
    public T[] mainDatas;

    public BaseArrayAdapter(Context context, T[] datas) {
        super();
        this.context = context;
        this.mainDatas = datas;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mainDatas.length;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }


    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    /**
     * 建议使用getObject替代
     */
    @Deprecated
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mainDatas[position];
    }

    public T getObject(int position) {
        // TODO Auto-generated method stub
        return mainDatas[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public T[] getMainDatas() {
        return mainDatas;
    }

    public void setMainDatas(T[] mainDatas) {
        this.mainDatas = mainDatas;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = View.inflate(context, getLayoutId(), null);
            BaseViewHolder holder = getHolder();
            holder.convertView = convertView;
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
            convertView.setOnClickListener(holder);

        }
        BaseViewHolder holder = (BaseViewHolder) convertView.getTag();
        holder.position = position;
        processView(holder);
        return convertView;
    }

    /**
     * 返回对应的Hodler.
     *
     * @return
     * @time 2015年3月5日
     */
    public abstract BaseViewHolder getHolder();

    /**
     * 处理每次GetView的数据
     *
     * @param baseViewHolder
     * @time 2015年3月5日
     */
    public abstract void processView(BaseViewHolder baseViewHolder);

    /**
     * 返回布局id
     *
     * @return
     * @time 2015年3月5日
     */
    public abstract int getLayoutId();

    public class BaseViewHolder implements View.OnClickListener {
        public int position;
        public View convertView;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            onItemClick(position, v);
        }
    }

    OnItemClickListener mOnItemClickListener;

    public OnItemClickListener getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void onItemClick(int position, View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.OnItemClick(this, position, v, getObject(position));
        }
    }

    public interface OnItemClickListener<T> {
        public void OnItemClick(BaseArrayAdapter<T> adapter, int position, View v, T t);
    }
}
